/*
 * @Author: cmd
 * @we_chat: cmd_develop
 * @Date: 2019-01-03 11:59:50
 * @LastEditors: cmd
 * @LastEditTime: 2019-01-05 09:16:08
 * @Description: 弹窗管理器
 */

const {ccclass, property} = cc._decorator;

@ccclass
export default class win_manager extends cc.Component {
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }

    loadPrefabNode(path, cb) {
        console.log("loadPrefabNode 加载资源");
        cc.loader.loadRes("prefabs/" + path, function (err, res) {
            if(err) {
                console.error("load_prefab err:",err);
                return;
            }
            let node = cc.instantiate(res);
            if (node == undefined) {
                console.error('loadPrefab undefined path = '+path);
            } else {
                cb && cb(node);
            }
        });
        console.log("loadPrefabNode 加载完成");
    }

    /**
     * 
     * @param win_name 窗口名字
     * @param open_type 窗口以何种模式打开
     */
    win_open(win_name:string,open_type:number){
        let win_path:string = 'win/' + win_name;
        let temp_node = cc.find("Canvas").getChildByName(win_name);       
        if (temp_node === null) { 
            this.loadPrefabNode(win_path, function (temp_node) {
                console.log('加载窗口');                 
                cc.find("Canvas").addChild(temp_node,100,win_name); 
            }.bind(this));
        } 
        else {   
            console.log('窗口名字已被占用')
        }
    }

    /**
     * 
     * @param win_name 窗口名字
     * @param close_type 窗口以何种模式关闭
     */
    win_close(win_name:string,close_type:number){
        let win_path:string = 'win' + win_name;
        let temp_node = cc.find("Canvas").getChildByName(win_name); 
        if (temp_node === null) { 
            console.log('窗口名字不存在')
        } 
        else {   
            temp_node.destroy(); 
        }
    }
    // update (dt) {}
}
